Fix ReduceLROnPlateau scheduler error when validation doesn't run every epoch #21266
+48
−4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fixes a
MisconfigurationException
that occurs when usingReduceLROnPlateau
scheduler withcheck_val_every_n_epoch > 1
. The scheduler was attempting to access validation metrics on epochs where validation didn't run, causing an error.Issue
When
check_val_every_n_epoch
is set to a value greater than 1, validation only runs on specific epochs (e.g., every 2nd epoch). However, theReduceLROnPlateau
scheduler was being updated at the end of every epoch, attempting to access the monitored metric (e.g.,val/loss
) even when it wasn't available.Example error:
Before submitting
Solution
Modified the scheduler update logic in
fit_loop.py
to only update plateau schedulers when validation actually runs. This ensures the monitored metrics are available when the scheduler needs them.Before:
After:
Testing
Added test case
test_reducelronplateau_with_check_val_every_n_epoch
that verifies the fix works correctly when validation runs every N epochs.Impact
"strict": False
to avoid the errorFixes #<issue_number>
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.
📚 Documentation preview 📚: https://pytorch-lightning--21266.org.readthedocs.build/en/21266/